package com.zhonghui.modlules.shop.eshopService.Ipml;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eshop.service.Impl.BaseServiceImpl;
import com.zhonghui.common.eshopEnum.CommonEnum;
import com.zhonghui.common.eshopMapper.SystemGroupDataMapper;
import com.zhonghui.common.eshopUtil.QueryHelpPlus;
import com.zhonghui.modlules.shop.eshopDomain.SystemGroupData;
import com.zhonghui.modlules.shop.eshopDto.SystemGroupDataQueryCriteria;
import com.zhonghui.modlules.shop.eshopService.SystemGroupDataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(
        propagation = Propagation.SUPPORTS,
        readOnly = true,
        rollbackFor = {Exception.class}
)
public class SystemGroupDataServiceImpl extends BaseServiceImpl<SystemGroupDataMapper, SystemGroupData> implements SystemGroupDataService {


    public List<SystemGroupData> queryAll(SystemGroupDataQueryCriteria criteria) {
        return ((SystemGroupDataMapper)this.baseMapper).
                selectList( QueryHelpPlus.getPredicate(SystemGroupData.class, criteria));
    }

    public List<JSONObject> getDatas(String name) {
        LambdaQueryWrapper<SystemGroupData> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SystemGroupData> wrapper = queryWrapper.eq( SystemGroupData::getGroupName, name ).eq( SystemGroupData::getStatus, CommonEnum.SHOW_STATUS_1.getValue() )
                .orderByAsc( SystemGroupData::getSort );
        List<SystemGroupData> systemGroupDatas = ((SystemGroupDataMapper)this.baseMapper).
                selectList(wrapper);
        List<JSONObject> list = (List)systemGroupDatas.stream().map(SystemGroupData::getValue).map( JSON::parseObject).collect( Collectors.toList());
        return list;
    }
}
